package smw;

import robocode.HitRobotEvent;
import java.awt.Color;
import robocode.HitByBulletEvent;
import robocode.Robot;
import robocode.ScannedRobotEvent;
import static robocode.util.Utils.normalRelativeAngleDegrees;

/**
 * Robot that moves semi-randomly and fires when it sees a robot.
 * Fire power is calculated by the enemy's distance from BlackMagic.
 * @author  Scott Wong
 * @version 1.1.929
 */
public class BlackMagic extends Robot {
  
  //variables used to move the turret
  double absoluteBearing;
  double bearingFromGun;
  
  /**
   * Runs the robot.
   */
  public void run() {
    //how far each move should be
    double moveAmount = (getBattleFieldWidth() / 2);
    
    //set the robot's colors
    setBodyColor(Color.black);
    setGunColor(Color.black);
    setRadarColor(Color.green);
    setBulletColor(Color.yellow);
    
    //moves to the middle and sets direction
    goMid();

    while (true) {
      ahead(moveAmount);
      turnRight(180);
      ahead(moveAmount);
      turnRight(135);
      ahead(moveAmount);
      turnRight(180);
      ahead(moveAmount);
      turnLeft(90);
    }
  }
  
  /**
   * Moves the robot the top middle of the field and sets the direction. 
   */
  public void goMid() {
    turnLeft(getHeading() % 90);
    ahead(getBattleFieldHeight());
    turnGunRight(90);
    turnRight(90);
    ahead((getBattleFieldWidth() / 2) - getX());
    //sets up for next movement
    turnRight(135);
  }
  
  /**
   * Calculates the angle to fire the gun.
   * @param heading BlackMagic's current heading.
   * @param bearing the enemy's current bearing.
   * @param gunHeading the heading of BlackMagic's gun.
   * @return the angle to turn the gun.
   */
  public double gunAngle(double heading, double bearing, double gunHeading) {
    // Calculate exact location of the robot
    //following 2 lines taken from TrackFire.java robot
    absoluteBearing = heading + bearing;
    bearingFromGun = normalRelativeAngleDegrees(absoluteBearing - gunHeading);
    return bearingFromGun;
  }

  /**
   * Fires on a robot depending on distance.
   * @param e a scanned robot.
   */
  public void onScannedRobot(ScannedRobotEvent e) { 
    // Calculate exact location of the robot
    bearingFromGun = gunAngle(getHeading(), e.getBearing(), getGunHeading());
    
    //if the robot is too close
    if (e.getDistance() < 30) {
      fire(3);
      back(50);
      turnLeft(30);
    }
    else {  //if the robot is a safe distance
      //adjust the gun for moving targets
      turnGunRight(bearingFromGun + 10);
      fire(2);
    }
    scan();
  }
  
  /**
   * Fires and moves away from the robot then fires on it again.
   * @param e a collision with another robot
   */
  public void onHitRobot(HitRobotEvent e) {
    // Calculate exact location of the robot
    bearingFromGun = gunAngle(getHeading(), e.getBearing(), getGunHeading());
    turnGunRight(bearingFromGun);
    
    //fire then move away and fire again
    fire(3);
    back(80);
    turnRight(60);
    
    // Calculate exact location of the robot
    bearingFromGun = gunAngle(getHeading(), e.getBearing(), getGunHeading());
    turnGunRight(bearingFromGun);
    fire(3);
  }
  
  /**
   * When a bullet hits BlackMagic.
   * @param e a hit by a bullet from another robot
   */
  public void onHitByBullet(HitByBulletEvent e) {
    turnLeft(30);
  }
}